package com.tg.app.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tg.app.R;

import java.util.Objects;

public class VerifyCodeView extends RelativeLayout {
    private final MyEditText editText;
    private final TextView[] textViews;
    private final View[] lineViews;
    private static final int MAX = 6;
    private String inputContent;

    public VerifyCodeView(Context context) {
        this(context, null);
    }

    public VerifyCodeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerifyCodeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.view_verify_code, this);

        textViews = new TextView[MAX];
        textViews[0] =  findViewById(R.id.tv_0);
        textViews[1] =  findViewById(R.id.tv_1);
        textViews[2] =  findViewById(R.id.tv_2);
        textViews[3] =  findViewById(R.id.tv_3);
        textViews[4] =  findViewById(R.id.tv_4);
        textViews[5] =  findViewById(R.id.tv_5);
        lineViews=new View[MAX];
        lineViews[0]=findViewById(R.id.line0);
        lineViews[1]=findViewById(R.id.line1);
        lineViews[2]=findViewById(R.id.line2);
        lineViews[3]=findViewById(R.id.line3);
        lineViews[4]=findViewById(R.id.line4);
        lineViews[5]=findViewById(R.id.line5);
        editText =  findViewById(R.id.edit_text_view);

        editText.setCursorVisible(false);//隐藏光标
        setEditTextListener();
    }

    private void setEditTextListener() {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                inputContent = Objects.requireNonNull(editText.getText()).toString();

                if (inputCompleteListener != null) {
                    if (inputContent.length() >= MAX) {
                        inputCompleteListener.inputComplete();
                    } else {
                        inputCompleteListener.invalidContent();
                    }
                }

                for (int i = 0; i < MAX; i++) {
                    if (i < inputContent.length()) {
                        textViews[i].setText(String.valueOf(inputContent.charAt(i)));
                        lineViews[i].setBackgroundColor(getResources().getColor(R.color.code_line));
                    } else {
                        textViews[i].setText("");
                        lineViews[i].setBackgroundColor(getResources().getColor(R.color.translucent_white));
                    }
                }
            }
        });
    }


    private InputCompleteListener inputCompleteListener;

    public void setInputCompleteListener(InputCompleteListener inputCompleteListener) {
        this.inputCompleteListener = inputCompleteListener;
    }

    public interface InputCompleteListener {

        void inputComplete();

        void invalidContent();
    }

    public String getEditContent() {
        return inputContent;
    }

}